const strats = {}
const LIFECYCLE = ["beforeCreate", "created", "beforeMount", "mounted"]
LIFECYCLE.forEach(fnName => {
  strats[fnName] = function (p, c) {
    if (c) {
      if (p) {
        return p.concat(c)
      } else {
        return [c]
      }
    } else {
      return p
    }
  }
})

strats.components = function (parentValue, childValue) {
  const res = Object.create(parentValue)
  if (childValue) {
    for (let key in childValue) {
      res[key] = childValue[key]
    }
  }
  return res
}

/**
 * 合并选项内容
 */
export function mergeOptions(parent, child) {
  let options = {}
  for (let key in parent) {
    mergeField(key)
  }
  for (const key in child) {
    if (!parent.hasOwnProperty(key)) {
      mergeField(key)
    }
  }

  function mergeField(key) {
    // 策略模式
    if (strats[key]) {
      options[key] = strats[key](parent[key], child[key])
    } else {
      options[key] = child[key] || parent[key]
    }
  }
  return options
}
